Page({
  data: {
   order_no: ''
  },
  onLoad(options) {
    let that = this;
    //路由参数获取
   that.setData(that.data.order_no = options.order_no);
    my.getAuthCode({
      scopes: 'auth_base',
      success: (res) => {
        if(res.authCode){  
            my.request({
            url: 'https://openpay.duolaibei.com/api/callbackal',
            data:{
              'code' : res.authCode,
              'order_no' : that.data.order_no ,
            },
            headers: {
                'Content-type': 'application/json'
            },
            method: 'POST',
            success: (result) => {
              if(result.data.code == 200){
                return new Promise((resolve, reject) => {
                    my.tradePay({
                      tradeNO: result.data.content.tradeNO,
                      success: (result) => {
                        if (result.resultCode != 9000) {
                          resolve({
                            status: false,
                            message: result.memo,
                            ...result
                          });
                        } else {
                          resolve({
                            status: true,
                            message: '支付成功',
                            ...result
                          });
                        }
                      },
                      fail: (err) => {   
                        reject({
                          status: false,
                          message: '支付异常',
                          ...err
                        });
                      },
                      complete:(e) =>{
                        console.log(e)
                          my.navigateTo({
                            url:'/pages/index/index'
                          });
                      }
                    });
                });

              }else{
                my.alert({
                 content: '支付异常2！',
                 success(){
                     my.navigateTo({
                        url:'/pages/index/index'
                     });
                 }
               });
            
              }
            },
            fail: (e) => {
               my.alert({
                 content: e,
               });
            },
            complete: (e) => {
            }
          });
        }else{
          my.alert({
            content: '授权失败！请联系商家',
          });
        }
      },
    })
  },

});
